﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Scenes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using GameEngine.animations;
using Imaginarium.team;

namespace Imaginarium.misc
{
    public class BattleDamageText: Text
    {
        #region Attributes
        public Character charSource { get; set; }
        private IGeneralListener listener = null;
        public Vector2 damageOrigin { get; set; }
        private const float maxTime = 80.0f;
        #endregion
        #region Constructors
        public BattleDamageText(Character charSource, SpriteFont font, string text, Vector2 position, Color color, IGeneralListener listener) :
            base(font, text, position, color, OriginType.CENTER)
        {
            this.charSource = charSource;
            this.damageOrigin = position;
            this.listener = listener;
        }
        #endregion
        #region Functionality
        public override void update(int dTime)
        {
            //base.update(dTime); guess it doesn't need it, and timeElapsed might ruined it if the text is flashing
            timeElapsed += dTime;
            if (color.A > 0)
            {
                position = calcPosition();
                color.A = calcAlpha();
                if (color.A < 0)
                {
                    color.A = 0;
                    if (listener != null)
                        listener.onSomethingFinished(this);
                }
            }
        }

        private Vector2 calcPosition()
        {
            float x = timeElapsed / 500.0f;
            float y = (float)(Math.Log(x) + 4)*-20;
            return damageOrigin + new Vector2(x, y);
        }

        private byte calcAlpha()
        {
            float x = timeElapsed / 30.0f;
            return (byte)((1 - (x / maxTime)) * 255);
        }
        #endregion
    }
}
